<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('投资年度')" prop="annualInvestment">
            <el-input v-model="form.annualInvestment" oninput="value=value.replace(/^\D*([0-9]\d*)?.*$/,'$1')" :placeholder="$t('投资年度')" show-word-limit />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('计划截止日期')" prop="planDeadline">
            <el-date-picker
              v-model="form.planDeadline"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('计划截止日期')"
              @change="planDeadlineChange"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注说明')" prop="descr">
            <el-input v-model="form.descr" type="textarea" :placeholder="$t('备注说明')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
      </el-row>
      <br>
      <br>
      <div class="area-header">
        <span class="area-header-title">{{ $t('送报机构') }}</span>
      </div>
      <el-table
        :data="form.investPlanSendingList"
        border
        class="nowrapTable formTable "
        style="overflow: auto;"
      >
        <el-table-column :label="$t('所属组织')" align="center" min-width="50">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('所属组织') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'investPlanSendingList.' + scope.$index + '.organisation' " :rules="PlanSendingRules.organisation">
              <DeptSelect v-model="scope.row.organisation" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('编制负责人')" align="center" min-width="50">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('编制负责人') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'investPlanSendingList.' + scope.$index + '.principalId' " :rules="PlanSendingRules.principalId">
              <UserSelect v-model="scope.row.principalId" :placeholder="$t('编制负责人')" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('备注')" align="center">
          <template slot-scope="scope">
            <el-form-item :prop="'investPlanSendingList.' + scope.$index + '.comment' ">
              <el-input v-model="scope.row.comment" type="textarea" class="fileTextarea" :placeholder="$t('备注')" show-word-limit maxlength="1000" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('操作')" align="center" width="80">
          <template slot-scope="scope">
            <el-button size="small" type="text" icon="el-icon-delete" @click="delHandle(scope.$index, scope.row)" />
          </template>
        </el-table-column>
      </el-table>
      <div class="tol">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="addSending">{{ $t("添加") }}</el-button>
      </div>
      <FileTable :list.sync="form.fileList" data-type="plan_issue" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="investPlan:issue" />
    </div>
  </div>
</template>

<script>
    import { saveInvestPlanIssue, getInvestPlanIssue } from '@/api/fund/investPlan/investPlanIssue'
    import { delInvestPlanSending } from '@/api/fund/investPlan/investPlanSending'
    export default {
        data() {
            return {
                // 表单参数
                form: {
                  // 上传
                  fileList: [],
                  // 送报机构
                  investPlanSendingList: []
                },
              // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  annualInvestment: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  planDeadline: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
                },
              PlanSendingRules: {
                organisation: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                principalId: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
              }
            }
        },
        created() {
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getInvestPlanIssue(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    annualInvestment: undefined,
                    planDeadline: undefined,
                    descr: undefined,
                    delFlag: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                      console.log(this.form.investPlanSendingList, 666)
                        saveInvestPlanIssue(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                }).catch(() => {
                    this.loading = false
                })
           },
          planDeadlineChange() {
              if (!this.form.id) {
              if (new Date(this.form.planDeadline) < new Date()) {
                this.$message({
                  type: 'warning',
                  message: this.$t('计划截止日期必须比当前日期晚')
                })
                this.form.planDeadline = undefined
              }
              } else {
                if (new Date(this.form.planDeadline) < new Date(this.form.createTime)) {
                  this.$message({
                    type: 'warning',
                    message: this.$t('计划截止日期必须比创建日期晚')
                  })
                  this.form.planDeadline = undefined
                }
              }
          },
          delHandle(index, row) {
            const id = row.id
            if (id !== undefined && id !== '' && id != null) {
              this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
                confirmButtonText: this.$t('确定'),
                cancelButtonText: this.$t('取消'),
                type: 'warning'
              })
                .then(function() {
                  return delInvestPlanSending(id)
                }).then(() => {
                this.form.investPlanSendingList.splice(index, 1)
                return this.msgSuccess(this.$t('删除成功'))
              })
                .catch(function() {})
            } else {
              this.form.investPlanSendingList.splice(index, 1)
            }
          },
          // 添加送报机构
          addSending() {
            const row = {
              id: undefined,
              issueId: undefined,
              organisation: undefined,
              principalId: undefined,
              comment: undefined
            }
            if (!(this.form.investPlanSendingList instanceof Array)) {
              this.$set(this.form, 'investPlanSendingList', [])
            }
            this.$nextTick(() => {
              this.form.investPlanSendingList.push(row)
            })
          }
        }
    }
</script>
